package rpa

import (
	"context"
	"errors"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/rpa"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/rpa"
	"git.myscrm.cn/golang/stark/v4"
)

/**
 * @Author: xiuliu
 * @Description:
 * @File:  rpa_start_server
 * @Version: 1.0.0
 * @Date: 2023/10/31 14:12
 */

type RpaStartServer struct {
	rpaService rpa.RpaServiceIface
}

func NewRpaStartServer(rpaService rpa.RpaServiceIface) pb.RpaStartServiceServer {
	return &RpaStartServer{
		rpaService: rpaService,
	}
}

func (r *RpaStartServer) RpaStart(ctx context.Context, request *pb.RpaStartRequest) (*pb.RpaStartResponse, error) {
	if request.UserMobile == "" {
		return nil, errors.New("手机号参数不能为空")
	}
	if request.SecrectKey == "" {
		return nil, errors.New("Rpa秘钥参数不能为空")
	}
	if request.Version == "" {
		return nil, errors.New("rpa协议版本参数不能为空")
	}
	if request.RpaVersion == "" {
		return nil, errors.New("Rpa客户端版本参数不能为空")
	}
	token, err := r.rpaService.RpaStart(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "Rpa启动验证失败", err.Error())
		return nil, err
	}
	resp := &pb.RpaStartResponse{
		Token: token,
	}

	return resp, nil
}

func (r *RpaStartServer) GetRpaStartInfo(ctx context.Context, request *pb.GetRpaStartInfoRequest) (*pb.GetRpaStartInfoResponse, error) {
	if request.Token == "" {
		return nil, errors.New("token参数不能为空")
	}
	token, err := r.rpaService.GetRpaStartInfo(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "获取Rpa验证信息失败", err.Error())
		return nil, err
	}
	resp := &pb.GetRpaStartInfoResponse{
		Token: token,
	}
	return resp, nil
}
